package com.example.orchid.paper.util;

import android.content.Context;
import android.os.Environment;
import android.util.Log;

import java.io.File;

/**
 * SD卡工具类
 * Created by Orchid on 2015-10-13.
 */
public class SDCardUtils {
    private static final String TYPE_CACHE = "cache";

    private static final String TYPE_FILES = "files";


    /**
     * 获取扩展存储file的绝对路径
     *
     * @param context
     * @return sdcard/Android/data/{package_name}/files/
     */
    public static String getExternalFileDir(Context context) {
        return getExternalDir(context, TYPE_FILES);
    }

    /**
     * 获取扩展存储cache的绝对路径
     *
     * 返回：sdcard/Android/data/{package_name}/cache/
     *
     * @param context
     * @return
     */
    public static String getExternalCacheDir(Context context) {

        return getExternalDir(context, TYPE_CACHE);
    }
    private static String getExternalDir(Context context, String typeFiles) {
        StringBuilder sb = new StringBuilder();

        if (context == null) {
            return null;
        }

        if (!isMounted()) {
            if (typeFiles.equals(TYPE_CACHE)) {
                sb.append(context.getCacheDir()).append(File.separator);
            } else {
                sb.append(context.getFilesDir()).append(File.separator);
            }

        }
        File file;

        if (typeFiles.equals(TYPE_CACHE)) {
            file = context.getExternalCacheDir();
        } else {
            file = context.getExternalFilesDir(null);
        }

        // In some case, even the sd card is mounted,
        // getExternalCacheDir will return null
        // may be it is nearly full.

        if (file != null) {
            sb.append(file.getAbsolutePath()).append(File.separator);
        } else {
            sb.append(Environment.getExternalStorageDirectory().getPath()).append("/Android/data/")
                    .append(context.getPackageName()).append("/").append(typeFiles).append("/")
                    .append(File.separator).toString();
        }
        Log.i("paper", sb.toString());
        return sb.toString();
    }

    /**
     * SD卡是否挂载
     *
     * @return
     */
    private static boolean isMounted() {
        String status = Environment.getExternalStorageState();
        return status.equals(Environment.MEDIA_MOUNTED);
    }


}
